<?php

namespace App\Services\ApplyGoldCoinWithdrawMyBank;

use Closure;
use App\Supports\Sms\Sms;
use App\Models\Withdraw;
use App\Http\Controllers\Api\V1\Requests\FormRequest;

class ApplyGoldCoinWithdrawMyBankRequest extends FormRequest
{
	public function rules()
	{
		$user = $this->user();
		if (! $user->cert_number) {
			fail('请先绑定银行卡账号');
		} 

		return [
			'sms_key' => 'required|string',
			'sms_code' => ['required', 'string', $this->smsCode()],
			'money' => [
				'required',
				'numeric',
				'min:1',
				$this->money()
			],
			

		];
	}

	public function money()
	{
		return function ($key, $value, $fail) {
			if ($value <= 0) {
				return $fail('请输入正确的提现金额');
			} elseif ($this->user()->balances <= 0) {
				return $fail('可提现余额不足');
			} elseif (! $this->isWithdrawOn()) {
				return $fail('平台尚未开放提现功能');
			} elseif (! $this->user()->isBankBound()) {
				return $fail('提现银行卡未绑定');
			} elseif (! $this->isBankLowestAmountMatched()) {
				return $fail("{$this->allowedBankLowestAmount()}元起提");
			}  elseif (! $this->isHighestAmountMatched()) {
				return $fail("提现金额必须小于 '{$this->allowedHighestAmount()}'");
			}  elseif (! $this->isGoldCoinsEnough()) {
				return $fail('可提现余额不足');
			}  elseif (request()->user()->white()->first() && request()->user()->white()->first()->is_withdraw_open == false) {
				return $fail('当前用户无法提现');
			/*}  elseif (! $this->user()->isUserUnusual()) {
				return $fail('可提现账户异常');*/
			/*}elseif($this->user()->balance  - $this->user()->prize < $value  && $this->user()->is_recycle == 0){
				return $fail("体验金金额部分不可提现");*/
			/*}  elseif (! $this->user()->hasSeckillOrder()) {
				return $fail('需要参加过一次秒杀（新人特权商品除外）后开启提现资格');*/
			}  else {
				return true;
			}
		};
	}

	public function smsCode(): Closure
	{
		return function ($attribute, $value, $fail) {
			$sms = app(Sms::class);
			$verification = $sms->isSmsDataRight(request('sms_key'),$this->user()->phone, $value);
			
			$key = "withdraw_".$this->user()->phone;

			if($sms->isFailCountOver($key)){

				return $fail('验证码申请过于频繁，请稍后再试');
			}

			if (! $verification) {
				$sms->incrFailCount($key);	
				return $fail('短信验证码无效');

			}
			return true;
		};
	}

	protected function withdrawTimeAllowedRange()
	{
		return Withdraw::withdrawTimeAllowedRange();
	}

	protected function isWithdrawTimeAllowed()
	{
		return Withdraw::isWithdrawTimeAllowed();
	}

	protected function isStormSupportMatched(): bool
	{
        $supportCoins = cs('storm')->gold_support_coins;

        if ($supportCoins <= 0) return true;

		/**
		 * @var User $user
		 */
		$user = $this->user();

		$hasSupported = $user->goldCoins()->where('remark', 'support_gold_coins')->exists();

		if (!$hasSupported) return true;

		$isLeftCoinsMatched = big_decimal($user->gold_coins)->minus($this->coins)->isGreaterThanOrEqualTo($supportCoins);

		if ($isLeftCoinsMatched) return true;

		return false;
	}

	protected function isWithdrawOn()
	{
		return Withdraw::isWithdrawOn();
	}

	protected function allowedLowestAmount()
	{
		return Withdraw::allowedLowestAmount();
	}

	protected function allowedBankLowestAmount()
	{
		return Withdraw::allowedBankLowestAmount();
	}

	protected function isLowestAmountMatched()
	{
		return Withdraw::isLowestAmountMatched($this->money);
	}

	protected function isBankLowestAmountMatched()
	{
		return Withdraw::isBankLowestAmountMatched($this->money);
	}

	protected function allowedHighestAmount()
	{
		return Withdraw::allowedHighestAmount();
	}

	protected function isHighestAmountMatched()
	{
		return Withdraw::isHighestAmountMatched($this->money);
	}

	protected function isBalanceEnough()
	{
		return Withdraw::isBalanceEnoughToDeduct($this->user(), $this->money);
	}

	
	protected function isGoldCoinsEnough()
	{
		return Withdraw::isCoinsEnoughToDeduct($this->user(), $this->money);
	}


}
